package com.glorysoft.knowlespda.ui.setting

import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.databinding.ViewDataBinding
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.customview.customView
import com.afollestad.materialdialogs.customview.getCustomView
import com.afollestad.materialdialogs.lifecycle.lifecycleOwner
import com.glorysoft.knowlespda.BR
import com.glorysoft.knowlespda.R
import com.glorysoft.knowlespda.base.BaseActivity
import com.glorysoft.knowlespda.base.BaseViewModel
import com.glorysoft.knowlespda.base.Constant
import com.glorysoft.knowlespda.databinding.ActivityPairingBinding
import kotlinx.android.synthetic.main.activity_pairing.*

/**
 * FileName: PairingActivity
 * Author: XJH
 * Date: 2022/7/8 11:52
 * Description:
 * History:
 */
class PairingActivity : BaseActivity<SettingViewModel,ActivityPairingBinding>(),
    View.OnClickListener {
    override fun layoutId(): Int {
        return R.layout.activity_pairing
    }

    override fun initVariableId(): Int {
       return BR.pairingViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(this.getString(R.string.machine_pairing))
        viewModel.getAuthority()

        tvUnPartial.setOnClickListener(this)
        tvNew.setOnClickListener(this)

    }

    override fun initData() {
        viewModel.defUI.callObserve.observe(this){
            tvPdaNo.text = viewModel.pdaNo
            tvPair.text = this.getString(R.string.pairmachine)+"(" + viewModel.pairItems.size + ")"
        }
    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.tvUnPartial ->{
                viewModel.unPair()
            }
            R.id.tvNew->{
                getCustomDialog().show()
            }
        }
    }

    private fun getCustomDialog(): MaterialDialog {
        val dialog =  MaterialDialog(this)
            .cancelable(false)
            .cornerRadius(12f)
            .customView(R.layout.dialog_scan_start, noVerticalPadding = true)
            .lifecycleOwner(this)
        val contentView = dialog.getCustomView()
        val ivClose: ImageView = contentView.findViewById(R.id.ivClose)
        val etPair: EditText = contentView.findViewById(R.id.etPair)
        val tvConfirm: TextView = contentView.findViewById(R.id.tvConfirm)

        customEdit(etPair,object : EditTextListener{
            override fun onTextChangeListener(s: String) {
                etPair.setText(etPair.text.toString() + ";")
                etPair.setSelection(etPair.text.length)
            }

        })

        ivClose.setOnClickListener {
            dialog.dismiss()
        }

        tvConfirm.setOnClickListener {
            viewModel.addPair(etPair.text.toString())
            dialog.dismiss()
        }

        return dialog
    }
}